package com.shrct.businessprocess.purchase.impl;

import com.shrct.businessprocess.diagnose.IDiagnoseAcceptor;
import com.shrct.businessprocess.money.Moneys;
import com.shrct.businessprocess.project.RCExchangeRate;
import com.shrct.businessprocess.project.RCProjectMoney;
import com.shrct.businessprocess.purchase.IHasServicePurchase;
import com.shrct.businessprocess.purchase.RCHasPurchaseBudget;
import com.shrct.businessprocess.purchase.RCServiceProductPurchaseRequest;
import com.shrct.businessprocess.purchase.RCServicePurchaseOrder;
import com.shrct.businessprocess.purchase.impl.RCBasePurchaseRequestImpl;
import com.shrct.businessprocess.purchase.impl.RCServicePurchaseOrderImpl;
import com.shrct.businessprocess.purchase.proxy.RCServiceProductPurchaseRequestProxy;
import edu.fudan.mylang.pf.IObjectFactory;
import java.util.Collection;
import javax.persistence.CascadeType;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.OneToMany;
import org.eclipse.xtext.xbase.lib.Functions.Function1;
import org.eclipse.xtext.xbase.lib.IterableExtensions;
import org.eclipse.xtext.xbase.lib.ObjectExtensions;
import org.eclipse.xtext.xbase.lib.Procedures.Procedure1;

@Entity
@DiscriminatorValue("RCSERVICEPRODUCTPURCHASEREQUEST_TYPE")
public class RCServiceProductPurchaseRequestImpl extends RCBasePurchaseRequestImpl implements RCServiceProductPurchaseRequest {
  public RCServiceProductPurchaseRequestImpl() {
    super();
  }
  
  public RCServiceProductPurchaseRequestImpl(final RCHasPurchaseBudget budget) {
    super( budget);
    
  }
  
  @OneToMany(targetEntity = RCServicePurchaseOrderImpl.class, cascade = { CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REMOVE }, mappedBy = "purchaseRequest")
  private Collection<RCServicePurchaseOrder> purchaseOrders = new java.util.ArrayList<com.shrct.businessprocess.purchase.RCServicePurchaseOrder>();;
  
  public Collection<RCServicePurchaseOrder> getPurchaseOrders() {
    return this.purchaseOrders;
  }
  
  public RCServicePurchaseOrder createAndAddtoPurchaseOrders() {
    getPurchaseOrders().size();
    com.shrct.businessprocess.purchase.RCServicePurchaseOrder rCServicePurchaseOrder = new com.shrct.businessprocess.purchase.impl.RCServicePurchaseOrderImpl(this);
    getObjectFactory().create(rCServicePurchaseOrder);
    getPurchaseOrders().add(rCServicePurchaseOrder);
    return rCServicePurchaseOrder;
    
  }
  
  public RCServicePurchaseOrder createAndAddtoPurchaseOrders(final Procedure1<RCServicePurchaseOrder> updater) {
    getPurchaseOrders().size();
    com.shrct.businessprocess.purchase.RCServicePurchaseOrder rCServicePurchaseOrder = new com.shrct.businessprocess.purchase.impl.RCServicePurchaseOrderImpl(this);
    getObjectFactory().create(rCServicePurchaseOrder);
    getPurchaseOrders().add(rCServicePurchaseOrder);
    return rCServicePurchaseOrder;
    
  }
  
  public RCServiceProductPurchaseRequest removeAllPurchaseOrders() {
    for(RCServicePurchaseOrder obj : this.purchaseOrders){
    	getObjectFactory().delete(obj);
    }
    this.purchaseOrders.clear();
    return this;	
    
  }
  
  public RCServiceProductPurchaseRequest removeFromPurchaseOrders(final RCServicePurchaseOrder rCServicePurchaseOrder) {
    this.purchaseOrders.remove(rCServicePurchaseOrder);
    getObjectFactory().delete(rCServicePurchaseOrder);
    return this;
    
  }
  
  public RCProjectMoney getTotalOrderAmount() {
    final Function1<RCServicePurchaseOrder,Boolean> _function = new Function1<RCServicePurchaseOrder,Boolean>() {
        public Boolean apply(final RCServicePurchaseOrder it) {
          Boolean _isInitialized = it.getIsInitialized();
          return _isInitialized;
        }
      };
    Iterable<RCServicePurchaseOrder> _filter = IterableExtensions.<RCServicePurchaseOrder>filter(this.purchaseOrders, _function);
    final Function1<RCServicePurchaseOrder,RCProjectMoney> _function_1 = new Function1<RCServicePurchaseOrder,RCProjectMoney>() {
        public RCProjectMoney apply(final RCServicePurchaseOrder it) {
          RCProjectMoney _amount = it.getAmount();
          return _amount;
        }
      };
    Iterable<RCProjectMoney> _map = IterableExtensions.<RCServicePurchaseOrder, RCProjectMoney>map(_filter, _function_1);
    RCExchangeRate _exchangeRate = this.getExchangeRate();
    RCProjectMoney _sum = Moneys.sum(_map, _exchangeRate);
    return _sum;
  }
  
  public void auditPassed() {
    this.setIsPassed(Boolean.valueOf(true));
  }
  
  public IHasServicePurchase getServiceBudget() {
    RCHasPurchaseBudget _budget = this.getBudget();
    return ((IHasServicePurchase) _budget);
  }
  
  public void diagnose(final IDiagnoseAcceptor acceptor) {
  }
  
  public RCServicePurchaseOrder createOrder() {
    RCServicePurchaseOrder _createAndAddtoPurchaseOrders = this.createAndAddtoPurchaseOrders();
    final Procedure1<RCServicePurchaseOrder> _function = new Procedure1<RCServicePurchaseOrder>() {
        public void apply(final RCServicePurchaseOrder it) {
          it.start();
        }
      };
    RCServicePurchaseOrder _doubleArrow = ObjectExtensions.<RCServicePurchaseOrder>operator_doubleArrow(_createAndAddtoPurchaseOrders, _function);
    return _doubleArrow;
  }
  
  public void setAsFinished() {
    this.setIsFinished(Boolean.valueOf(true));
  }
  
  public static RCServiceProductPurchaseRequest create(final RCHasPurchaseBudget budget, final IObjectFactory objectFactory) {
    com.shrct.businessprocess.purchase.RCServiceProductPurchaseRequest rCServiceProductPurchaseRequest = new com.shrct.businessprocess.purchase.impl.RCServiceProductPurchaseRequestImpl(
    	budget
    );
    objectFactory.create(rCServiceProductPurchaseRequest);
    return rCServiceProductPurchaseRequest;			
    
  }
  
  public static RCServiceProductPurchaseRequest createRCServiceProductPurchaseRequestImpl(final RCHasPurchaseBudget budget, final IObjectFactory objectFactory, final Procedure1<RCServiceProductPurchaseRequest> init) {
    com.shrct.businessprocess.purchase.RCServiceProductPurchaseRequest rCServiceProductPurchaseRequest = new com.shrct.businessprocess.purchase.impl.RCServiceProductPurchaseRequestImpl(
    	budget
    );
    objectFactory.create(rCServiceProductPurchaseRequest);
    init.apply(rCServiceProductPurchaseRequest);
    
    return rCServiceProductPurchaseRequest;			
    
  }
  
  public RCServiceProductPurchaseRequestProxy toProxy() {
    com.shrct.businessprocess.purchase.proxy.RCServiceProductPurchaseRequestProxy proxy = new com.shrct.businessprocess.purchase.proxy.RCServiceProductPurchaseRequestProxy();
    proxy.setId(this.getId());
    if(this instanceof edu.fudan.mylang.pf.INamedModelObject){
    	proxy.setName(((edu.fudan.mylang.pf.INamedModelObject)this).getEntityName());
    }
    return proxy;
    
  }
}
